package com.hgyc.mom.mes.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.common.enums.CommonStatus;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.mes.entity.MesWorkshop;
import com.hgyc.mom.mes.vo.MesWorkshopParam;
import com.hgyc.mom.mes.service.MesWorkshopService;
import com.hgyc.mom.mes.vo.MesWorkshopVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 车间 Controller
 *
 * @author fwj
 * @since 2025-06-23
 */
@Tag(name = "车间 控制器", description = "车间 控制器")
@RestController
@RequestMapping("/mes/workshop")
public class MesWorkshopController {
    @Autowired
    private MesWorkshopService mesWorkshopService;

    @Operation(summary = "查询车间 列表", description = "查询车间 列表")
    @PostMapping("/pageList")
    public Result<IPage<MesWorkshop>> pageList(@Parameter(description = "查询参数") @RequestBody MesWorkshopParam mesWorkshopParam) {
        Page<MesWorkshop> page = new Page<>(mesWorkshopParam.getPage(), mesWorkshopParam.getPageSize());
        MesWorkshop mesWorkshop = new MesWorkshop();
        BeanUtils.copyProperties(mesWorkshopParam, mesWorkshop);
        QueryWrapper<MesWorkshop> wrapper = QueryWrapperUtil.build(mesWorkshop);

        if (mesWorkshop.getParentId() != null) {
            wrapper.eq("parent_id", mesWorkshop.getParentId()).or().eq("id", mesWorkshop.getParentId());
        }

        IPage<MesWorkshop> list = mesWorkshopService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "查询车间树列表", description = "查询车间树列表")
    @PostMapping("/treeList")
    public Result<List<MesWorkshop>> treeList(@Parameter(description = "查询参数") @RequestBody MesWorkshopParam mesWorkshopParam) {
        List<MesWorkshop> list = mesWorkshopService.treeList(mesWorkshopParam);
        return Result.success(list);
    }

    @Operation(summary = "查询车间下拉列表", description = "查询车间下拉列表")
    @GetMapping("/selectList")
    public Result<List<MesWorkshop>> selectList() {
        LambdaQueryWrapper<MesWorkshop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MesWorkshop::getParentId, 0);
        wrapper.eq(MesWorkshop::getEnableFlag, CommonStatus.ENABLE.getValue());
        List<MesWorkshop> list = mesWorkshopService.list(wrapper);
        return Result.success(list);
    }

    @Operation(summary = "查询产线下拉列表", description = "查询产线下拉列表")
    @GetMapping("/line/selectList/{workshopId}")
    public Result<List<MesWorkshop>> lineSelectList(@PathVariable("workshopId") Long workshopId) {
        LambdaQueryWrapper<MesWorkshop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MesWorkshop::getParentId, workshopId);
        wrapper.eq(MesWorkshop::getEnableFlag, CommonStatus.ENABLE.getValue());
        List<MesWorkshop> list = mesWorkshopService.list(wrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取车间 详细信息", description = "获取车间 详细信息")
    @GetMapping(value = "/{id}")
    public Result<?> getInfo(@Parameter(description = "车间 ID") @PathVariable("id") Long id) {
        return Result.success(mesWorkshopService.getById(id));
    }

    @Operation(summary = "新增车间 ", description = "新增车间 ")
    @PostMapping
    public Result<?> save(@Parameter(description = "车间 ") @RequestBody MesWorkshopVO mesWorkshopVO) {
        mesWorkshopService.saveWorkshop(mesWorkshopVO);
        return Result.success();
    }

    @Operation(summary = "修改车间 ", description = "修改车间 ")
    @PutMapping
    public Result<?> update(@Parameter(description = "车间 ") @RequestBody MesWorkshopVO mesWorkshopVO) {
        mesWorkshopService.updateWorkshop(mesWorkshopVO);
        return Result.success();
    }

    @Operation(summary = "删除车间 ", description = "删除车间 ")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(mesWorkshopService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MesWorkshop> util = new ExcelUtil<>(MesWorkshop.class);
        util.importTemplateExcel(response, "车间 ");
    }

    @Operation(summary = "导出车间 列表", description = "导出车间 列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesWorkshopParam mesWorkshopParam) {
        MesWorkshop mesWorkshop = new MesWorkshop();
        BeanUtils.copyProperties(mesWorkshopParam, mesWorkshop);
        QueryWrapper<MesWorkshop> wrapper = QueryWrapperUtil.build(mesWorkshop);
        List<MesWorkshop> list = mesWorkshopService.list(wrapper);
        ExcelUtil<MesWorkshop> util = new ExcelUtil<>(MesWorkshop.class);
        util.exportExcel(response, list, "车间 数据");
    }

    @Operation(summary = "导入车间 列表", description = "导入车间 列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MesWorkshop> util = new ExcelUtil<>(MesWorkshop.class);
        List<MesWorkshop> mesWorkshopList = util.importExcel(file.getInputStream());
        String message = mesWorkshopService.importData(mesWorkshopList, updateSupport);
        return Result.success(message);
    }
}